<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="户号" prop="householdRegisterId">
        <el-select v-model="queryParams.householdRegisterId" clearable filterable placeholder="请选择户号">
          <el-option
            v-for="dict in residenceList"
            :key="dict.id"
            :label="dict.householdsNo"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="householdRegisterId">
        <el-select v-model="queryParams.status" clearable placeholder="请选择状态">
          <el-option
            v-for="dict in statusOption"
            :key="dict"
            :label="dict"
            :value="dict"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名"  prop="name">
        <el-input v-model="queryParams.name"></el-input>
      </el-form-item>
      <el-form-item label="身份证号"  prop="idCard" >
        <el-input v-model="queryParams.idCard"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['people:people:add']"
        >新登记</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['people:people:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="peopleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="曾用名" align="center" prop="formerName"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="籍贯" align="center" prop="regional"/>
      <el-table-column label="出生日期" align="center" prop="birthday" width="180"/>
      <el-table-column label="身份证号" align="center" prop="idCard"  show-overflow-tooltip/>
      <el-table-column label="与户主的关系" align="center" prop="relationship"/>
      <el-table-column label="文化程度" align="center" prop="educationLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.level_education" :value="scope.row.educationLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="户号" align="center" prop="householdsNo"/>
      <el-table-column label="来源" align="center" prop="residenceCategory"/>
      <el-table-column label="当前状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.people_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status=='正常'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['people:people:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.status=='正常'"

            size="mini"
            type="text"
            icon="el-icon-right"
            @click="handleDelete(scope.row)"
            v-hasPermi="['people:people:remove']"
          >迁出</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="户口类别" prop="residenceCategory">
          <el-radio-group v-model="form.residenceCategory" @change="handleResidenceCategory">
            <el-radio label="迁入"></el-radio>
            <el-radio label="暂住"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="户号" prop="householdRegisterId" v-if="showResidence">
          <el-select v-model="form.householdRegisterId" clearable filterable placeholder="请选择户号">
            <el-option
              v-for="dict in residenceList"
              :key="dict.id"
              :label="dict.householdsNo"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名"  prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="曾用名"  prop="formerName">
          <el-input v-model="form.formerName"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号"  prop="idCard">
          <el-input v-model="form.idCard"></el-input>
        </el-form-item>
        <el-form-item label="与户主的关系"  prop="relationship">
          <el-input v-model="form.relationship"></el-input>
        </el-form-item>
        <el-form-item label="籍贯"  prop="regional">
          <el-input v-model="form.regional"></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker clearable size="small"
                          v-model="form.birthday"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="文化程度"  prop="educationLevel">
          <el-select v-model="form.educationLevel" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.level_education"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改人员对话框 -->
    <el-dialog title="迁出" :visible.sync="outOpen" width="800px" append-to-body>
      <el-form ref="outForm" :model="outform" :rules="rules" label-width="120px">
        <el-form-item label="迁出原因"  prop="outReason">
          <el-input v-model="outform.outReason"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOutForm">确 定</el-button>
        <el-button @click="cancelOut">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPeople, getPeople, delPeople, addPeople, updatePeople, outPeople } from '@/api/people/people'
import { listAllResidence } from '@/api/residence/residence'

export default {
  name: "People2",
  dicts:['sys_user_sex','level_education','people_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员表格数据
      peopleList: [],
      statusOption:['正常','迁出'],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 户口类别
      residenceCategory: '',
      // 户籍列表
      residenceList:[],
      outform:{},
      outOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        formerName: null,
        sex: null,
        regional: null,
        nation: null,
        birthday: null,
        address: null,
        idCard: null,
        relationship: null,
        type: null,
        birthPlace: null,
        educationLevel: null,
        householdRegisterId: null,
        isOut: null
      },
      showResidence: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "人员姓名不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        outReason: [
          { required: true, message: "迁出原因不能为空", trigger: "blur" }
        ],
        residenceCategory: [
          { required: true, message: "类别不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    if(this.$route.query.householdRegisterId){
      this.queryParams.householdRegisterId = parseInt(this.$route.query.householdRegisterId)
    }
    this.getList();
    this.getResidence()
  },
  methods: {
    getResidence(){
      listAllResidence({status:'正常'}).then(response=>{
        this.residenceList = response.data
      })
    },
    /** 查询人员列表 */
    getList() {
      this.loading = true;
      listPeople(this.queryParams).then(response => {
        this.peopleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleResidenceCategory(val){
      if(val=='迁入'){
        this.showResidence = true
      }else {
        this.showResidence = false
        this.form.householdRegisterId = 0
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        residenceCategory: '迁入',
        formerName: null,
        sex: null,
        regional: null,
        nation: null,
        birthday: null,
        address: null,
        idCard: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        relationship: null,
        type: null,
        birthPlace: null,
        educationLevel: null,
        householdRegisterId: null,
        isOut: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新登记按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPeople(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePeople(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPeople(this.form).then(response => {
              this.$modal.msgSuccess("新登记成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 迁出按钮操作 */
    handleDelete(row) {
      this.outOpen = true
      this.outform.id = row.id
      this.outform.householdRegisterId = row.householdRegisterId
      // const ids = row.id || this.ids;
      // this.$modal.confirm('是否确认迁出？').then(function() {
      //   return delPeople(ids);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("迁出成功");
      // }).catch(() => {});
    },
    /** 提交按钮 */
    submitOutForm() {
      this.$refs["outForm"].validate(valid => {
        if (valid) {
            outPeople(this.outform).then(response => {
              this.$modal.msgSuccess("迁出成功");
              this.outOpen = false;
              this.getList();
            });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('people/people/export', {
        ...this.queryParams
      }, `people_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
